(function(){
	'use strict';

	var ALARM = angular. module('zhxCtcAlarmModule',[]);

	ALARM.controller('zhxCtcAlarmCtrl', ['$scope','$q',function($scope,$q){

		var alarmDataReady = $q.defer();

		$scope.gridColumn = $scope.options.columns;
		$scope.levelKey = $scope.options.levelKey;
		$scope.level = $scope.options.level;
		
		alarmDataReady.promise.then(function(alarmData){
			$scope.gridData = alarmData;
		});

		this.getOptions = function(){
			return $scope.options;
		};

		$scope.$watch('options.data',function(nv,ov){
			if( nv !== ov && nv ){
				alarmDataReady.resolve( nv );
			}
		});

	}]);

	ALARM.directive('zhxCtcAlarm', function(){
		return {
			restrict : 'E',
			scope : {
				options : '='
			},
			replace: true,
			controller : 'zhxCtcAlarmCtrl',
			templateUrl : 'zhx.ctc.alarm.vicco'
		}
	});

	//row
	ALARM.directive('zhxAlarmRow', ['$timeout',function($timeout){
		return{
			restrict : 'E',
			scope : true,
			require : '^zhxCtcAlarm',
			replace: true,
			link : function( scope, tElement, tAttrs, ctcAlarm ){
	
				scope.row = scope.$parent.row;
				scope.gridColumn = scope.$parent.gridColumn;

				//process event
				scope.processed = function(){
					tElement.velocity({ height : 0, opacity : 0 },{
						duration : 250,
						complete : function(){
							tElement.remove();
							scope.$parent.$destroy();
						}
					})
				
				};

				//event bind
				scope.processAlarm = function(event, rowData){
					var ev = event || window.event;
					var opts = ctcAlarm.getOptions();
					try{
						opts.event.call( this, scope, rowData, ev);
					}catch(e){
						//do not init options event key
						console.warn('请在options中设置event')
					}
				};

			},
			templateUrl : 'zhx.ctc.alarmRow.vicco'
		}
	}]);
	//cell
	ALARM.directive('zhxAlarmCell', ['$filter',function($filter){
		return{
			restrict:'E',
			scope: {
				row : '=',
				column : '='
			},
			replace: true,
			link : function( scope, tElement, tAttrs ){
				var row = scope.row,
					column = scope.column;

				if( !angular.isUndefined(scope.column) ){

					var filterType = typeof( column.filter );

                    switch (filterType) {
                        case 'string':
                            var filterHtml = $filter(column.filter)(row[column.name]);
                            row[column.name] = filterHtml;
                            break;
                        case 'object':
                            var filter = column.filter;
                            var filterHtml = $filter(filter.name)(row[column.name], filter.params[0], filter.params[1]);
                             row[column.name] = filterHtml;
                            break;
                    }
				}


			},
			templateUrl : 'zhx.ctc.alarmCell.vicco'
		}
	}])


})()